"""
测试元气潮盒抽奖逻辑
"""
import requests
import jsonpath
from common.logger import log

host = "http://micro.qsmy532.top/qknode-box"

def get_ch()->list:
    """获取潮盒分类id"""
    path = "/blindbox/getBlindBox"
    url = host+ path
    r = requests.get(url).json()
    id = jsonpath.jsonpath(r,"$...id")
    spuType = jsonpath.jsonpath(r,"$...spuType")
    return id,spuType

def get_shops(id)->list:
    """id：盲盒id"""
    path ="/blindboxbackground/getBlindBoxGoods"
    url = host + path
    data ={"spuId":id,"spuType":7,"pageNo":1,"pageSize":100}
    r = requests.get(url,params=data).json()
    shop_ids = jsonpath.jsonpath(r,"$...id")
    if len(shop_ids) > 0:
        #print(len(shop_ids))
        return shop_ids
    else:
        log.error(f"潮盒{id}下没有商品")

def get_fixedGoods(id)->list:
    """按盲盒id获取固定商品"""
    path = "/blindboxbackground/getBlindBoxGoods"
    url = host + path
    data = {"spuId":id,"spuType":7,"extract":"fixedGoods","pageNo":1,"pageSize":30}
    r = requests.get(url,params=data).json()
    #print(r)
    try:
        fixedgoods = jsonpath.jsonpath(r,"$..id")
        if fixedgoods:
            return fixedgoods
        else:
            log.error(f"盲盒{id}下固定商品不足，请检查")
            return []
    except Exception as e:
        log.error(f"盲盒{id}获取固定商品出错，请检查")
        print(e)


def get_extGoods(id)->list:
    """按盲盒id获取商品抽取池"""
    path = "/blindboxbackground/getBlindBoxGoods"
    url = host + path
    data = {"spuId":id,"spuType":7,"extract":"extPoolGoods","pageNo":1,"pageSize":30}
    r = requests.get(url,params=data).json()
    #print(r)
    try:
        extGoods = jsonpath.jsonpath(r,"$..id")
        if extGoods:
            return extGoods
        else:
            log.error(f"盲盒{id}下抽取池商品不足，请检查")
            return []
    except Exception as E:
        log.error(f"盲盒{id}获取抽取池商品出错，请检查")

def openBlindBox(id):
    """开盲盒,返回商品id"""
    path = "/blindbox/openBlindBox"
    url = host+path
    data = {"id":id,"guid":"kajsdajdja1231410999","rechargeType":"BOX_PRICE_TYPE_1_ONE"}
    r = requests.post(url,data=data).json()
    name = jsonpath.jsonpath(r,"$..prizeName")
    #print(name)
    ids = r["data"]["list"][0]["id"]
    if ids:
        return ids,name[0]
    else:
        log.error(f"盲盒{id}开盒出错，ids不存在")

def check_true_goods(id):
    """校验固定商品和抽取池"""
    shop_ids = get_shops(id)
    fixedGoods = get_fixedGoods(id)
    extGoods = get_extGoods(id)
    if fixedGoods:
        log.info(f"盲盒{id}的固定商品池：{fixedGoods}")
        if [False for i in fixedGoods if i not in shop_ids]:
            log.error(f"盲盒{id}的固定奖品不在其商品列表内")
    if extGoods:
        log.info(f"盲盒{id}的抽取池：{extGoods}")
        if [False for i in extGoods if i not in shop_ids]:
            log.error(f"盲盒{id}的抽取池不在其商品列表内")

def check_open_box(id):
    """校验开盲盒"""
    fixedGoods = get_fixedGoods(id)
    extGoods = get_extGoods(id)
    log.debug(f"盲盒{id}固定商品池：{fixedGoods}")
    log.debug(f"盲盒{id}抽取商品池：{extGoods}")
    ids_all = []
    for i in range(1,11):
        A = openBlindBox(id)
        #print(A)
        ids = A[0]
        name = A[1]
        if not ids:
            log.error(f"盲盒{id}第{i}次抽取：什么都没抽到{ids}")
        elif i in [1,5,10]:
            if ids in fixedGoods:
                log.info(f"盲盒{id}第{i}次抽取：抽到了【{name}】")
            else:
                log.error(f"盲盒{id}第{i}次抽取：抽到了【{name}:{ids}】,不在固定商品池内")
        elif i in [2,4,6,8,9] :
            if ids in extGoods:
                log.info(f"盲盒{id}第{i}次抽取：抽到了【{name}】")
            else:
                log.error(f"盲盒{id}第{i}次抽取：抽到了【{name}:{ids}】,不在抽取池内")
        elif i in [3]:
            if ids in extGoods and  ids not in ids_all:
                log.info(f"盲盒{id}第{i}次抽取：抽到了【{name}】")
            else:
                log.error(f"盲盒{id}第{i}次抽取：抽到了【{name}:{ids}】,不符合规则")
        elif i in [7]:
            if ids in extGoods and ids in ids_all:
                log.info(f"盲盒{id}第{i}次抽取：抽到了【{name}】")
            else:
                log.error(f"盲盒{id}第{i}次抽取：抽到了【{name}:{ids}】,不符合规则")
        ids_all.append(ids)
    log.debug(f"盲盒{id}十次抽取结果：{ids_all}")
    log.info("-"*100)

if __name__ == '__main__':
    #check_true_goods(1)
    #获取盲盒id
    ids = get_ch()[0]
    log.info(f"盲盒数量："+str(len(ids)))
    for id in ids:
        #check_true_goods(id)
        check_open_box(id)